## Include a new sensor ##

If you have tested a sensor driver working on AVR 8 bits MCU before
, that driver can be adapted to be used in EMMA , read this.

First the sensor driver should implement [these functions](src/README.md#FunctionsToImplement) if already done then follow these.

### sensor flag ###

Define a macro for the sensor's bit to be identified on file [sensors_flags](src/sensors_flags.h) like:

`#define DHT11 1`

That means the bit `1` will be addresed to refer the sensor DHT11, the sensor DHT11 measures temperature so the byte `TEMPERATURE_CTRL_BYTE1` will be used in EEPROM memory to check the state of sensors
realted to temperature.

### sensor's name string ###

You should have defined a string containing the sensor's name on
file [ProgMemStrings.h](src/ProgMemStrings.h), for instance:

`const char STR_dht11[] PROGMEM = "DHT11";`

In the same file there should be an array which **contains** the
string you just defined. Remeber the bit defined in [sensors_flags](../src/sensors_flags.h) for the sensor you are to add, that same
number should be the number of the element in the array.

For instance, the array that contains sensor names that measure temperature is `STR_SensorsNamesTemperature1[]` and the string corresponding the sensor `STR_dht11` is at position `1` (1 was the defined bit in `sensor_flags.h` file for this sensor). The array should be like this:

    PGM_P const STR_SensorsNamesTemperature1[] PROGMEM ={
      // Temperature
      STR_lm35, //0 
      STR_dht11, //1 <-- String corresponding to the sensor
      STR_ds18s20, //2
    };


If in doubt about this see [ProgMemStrings.h](src/ProgMemStrings.h)

### Sensor Initialization ###

A sensor main header should be included in [sensors.h](src/sensors.h) see that file and include the new sensor.

But [sensors.c](src/sensors.c) contains a function `void InitializeSensors(void)`. This function **calls the sensor's** `init` function. Include the `init` function of the sensor you want to add there, to make sure the sensor was marked to be included in logs you should add an if statement that checks if so, like this one:

    if(temperature_ctrl & (1<<DHT11))
        dht11_init();

The main program calls this function to initialize all sensors, and with the last statement in `InitializeSensors` function, the sensor will initialize properly.

### Log the sensor's response and name ###

#### Sensor's name ####

The function `LogIntoFile()` on [DataLogger.c](src/DataLogger.c) file is  the function that actually **reads the sensor's name** you defined on [ProgMemStrings.h](src/ProgMemStrings.h) and logs it in a file.

For instance, if the sensor measures temperature, there should be an if statement `if(temperature_ctrl)` which checks if a sensors that measures temperature was marked. You should nest there another if statement that checks if the sensor was marked to be included in the log. The statement should result in:

    if(temperature_ctrl & (1<<DHT11))
    {
        Copy_STR_SensorsNamesTemperature1(str_buff, DHT11);
        sprintf(str_buff, "%s\t", str_buff);
		addToNewContentBuffer(str_buff, 1);
    }

It will check for DHT11 sensor's `control_flag` then it will copy the sensor's name to `str_buff` string , then it will **append** that string into a temporal buffer, that buffer will later be logged on a file.

#### Sensor's response ####

The function `LogIntoFile()` on [DataLogger.c](src/DataLogger.c) file is  the function that actually **calls the sensor's** `get_response` function.

There is a *second if statement* that checks for physical phenomenom byte in the same file, you should nest there another if statement that checks wether the sensor was marked to be included in the logs. To do so for DHT11 sensor the statement will look like:

    if(temperature_ctrl & (1<<DHT11))
    {
        last_sensor_response = dht11_get_Temperature();
		AppendAndCheckValidDataFromSensor(last_sensor_response, logfile->FileName);
    }


It checks if the sensor DHT11 (which measures temperature) was marked, then it *calls the sensor's* `get_response` storing its response in `last_sensor_response` var. Then it checks if that response is valid and *appends* that response into a temporal buffer that will be stored on a file later.

*That is all needed to do for a sensor to be added using Datalogger system*

